home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / dbf_tc.zip / D_OPEN.C < prev    next >
Text File  |  1987-06-18  |  4KB  |  126 lines

  1. /* 
  2. **        file:            d_open.c
  3. **        purpose:        routine to open a dbaseiii file for access by the other routines
  4. **                        in dbf.lib.
  5. **        usage:        d = (struct DBF *)malloc(sizeof(struct DBF));
  6. **                        strcpy(d->filename,"filename.dbf");
  7. **                        d_open(d);
  8. **                        ... access file with other routines ...
  9. **                        d_close(d);
  10. **                        free(d);
  11. **        notes:        compile with "tcc -c d_open".  include this file in dbf.lib
  12. **                        see dbf.h for structure of DBF.  copy filename into structure
  13. **                        before calling d_open.
  14. **        returns:        0             if successful with structure filled in
  15. **                        NO_FILE     if unable to find file
  16. **                        OUT_OF_MEM if not enough memory
  17. **                        BAD_FORMAT if not dBASE file
  18. **        author:         Mark Sadler
  19. **        revised:        6/18/87
  20. */ 
  21.  
  22. #include <stdio.h>
  23. #include <alloc.h>
  24. #include "dbf.h"
  25.  
  26. int d_open(struct DBF *d)
  27. {
  28.     int i;
  29.     int n;
  30.  
  31.     d->status = not_open;                             /* in case can not open       */
  32.     if((d->file_ptr = fopen(d->filename,"r+b")) == NULL)
  33.         return(NO_FILE);
  34.  
  35.     rewind(d->file_ptr);
  36.  
  37.     fread((void *)&d->dbf_version,(unsigned)1,(unsigned)12,d->file_ptr);    /* read prolog                 */
  38.  
  39.  
  40.     if (d->dbf_version != DB3FILE                    /* check for dbiii file marker */
  41.         && d->dbf_version != DB3WITHMEMO
  42.         || d->update_mo == 0)
  43.             {
  44.             fclose(d->file_ptr);
  45.             return(BAD_FORMAT);
  46.             }
  47.  
  48.     d->current_record = 0L;
  49.     d->num_fields = ((d->header_length - (FIELD_REC_LEN+1)) / HEADER_PROLOG);
  50.  
  51.     if((d->fields_ptr = (struct FIELD_RECORD *)malloc((unsigned)(d->num_fields * FIELD_REC_LEN)))==NULL)
  52.         return(OUT_OF_MEM);
  53.  
  54.     /* position at field descriptions */
  55.     fseek(d->file_ptr,(long)HEADER_PROLOG,0);
  56.     
  57.     /* read into field description array */
  58.     fread((void *)d->fields_ptr,sizeof *d->fields_ptr,(unsigned)d->num_fields,d->file_ptr); 
  59.  
  60.     if((d->record_ptr = (char *)malloc(d->record_length))==NULL)
  61.         return(OUT_OF_MEM);
  62.  
  63.     /* initialize pointers to fields in record. */
  64.     for(i=0,n=1;i<d->num_fields;i++)        /* n is offset from start of rec     */
  65.         {                                             /* @ n=0 is the deleted record flag  */
  66.         d->fields_ptr[i].field_data_address = d->record_ptr + n;
  67.         n += d->fields_ptr[i].len;
  68.         }
  69.  
  70.     d->status = not_updated;                         /* open successfull */
  71.     return(0);
  72. }
  73.  
  74. #ifdef DEBUG_MAIN        /* test program */
  75. main(int argc,char **argv)
  76. {
  77.     struct DBF d;
  78.     int errornum;
  79.     int i;
  80.  
  81.     if (argc != 2)
  82.         {
  83.         printf("Usage d_open filename");
  84.         exit(1);
  85.         }
  86.  
  87.     strcpy(d.filename,argv[1]);
  88.     if(!strchr(d.filename,'.'))                                         /* default to .dbf file     */
  89.         strcat(d.filename,".DBF");
  90.  
  91.     if((errornum = d_open(&d))!=0)                                                 /* open file                            */
  92.     {
  93.         printf("Error opening file: ");
  94.         switch (errornum)
  95.         {
  96.             case OUT_OF_MEM:
  97.                 printf("Not enough memory.\n");
  98.                 break;
  99.             case NO_FILE:
  100.                 printf("Can not open file %s.\n",d.filename);
  101.                 break;
  102.             case BAD_FORMAT:
  103.                 printf("File %s is not a dBASE III file.\n",d.filename);
  104.                 break;
  105.         }
  106.         exit(1);
  107.     }
  108.     
  109.     printf("\nFile Name: %s",d.filename);
  110.     printf("\nCurrent Record: %ld",d.current_record);
  111.     printf("\nDBF signature: %d",d.dbf_version);
  112.     if (d.status==not_updated)
  113.         printf("\nStatus Not Updated");
  114.     else
  115.         printf("\nStatus Not Open");
  116.     printf("\nNumber of Fields: %d",d.num_fields);
  117.     printf("\nUpdate Year: %d",d.update_yr);
  118.     printf("\nUpdate Month: %d",d.update_mo);
  119.     printf("\nUpdate Day: %d",d.update_day);
  120.     printf("\nNumber of Records: %ld",d.records);
  121.     printf("\nRecord Length: %d",d.record_length);
  122.     for (i=0;i<d.num_fields;i++)
  123.         printf("\nField #%d: %s",i,d.fields_ptr[i].name);
  124. }
  125. #endif
  126.